(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var self = plus.webview.currentWebview();
		var opener = plus.webview.getWebviewById('order_list');
		//var opener = self.opener();
		var oid = self.oid;

		//console.log(oid);

		function uploadImg(path) {
			var img = new Image();
			img.src = path;
			img.onload = function() {
				var that = this;
				//生成比例 
				var w = that.width,
					h = that.height,
					scale = w / h;
				w = 480 || w; //480  你想压缩到多大
				h = w / scale;

				//生成canvas
				var canvas = document.createElement('canvas');
				var ctx = canvas.getContext('2d');
				canvas.setAttribute('width', w);
				canvas.setAttribute('height', h);
				ctx.drawImage(that, 0, 0, w, h);
				var base64 = canvas.toDataURL('image/jpeg', 1 || 0.8); //1z 表示图片质量，越低越模糊。	 
				//console.log(base64);
				//base64 = base64.replace('data:image/jpeg;base64,', '');
				$.post(upurl, {
					file: base64,
					uid: uid,
					path: path
				}, function(res) {
					console.log(res);
					res = JSON.parse(res);

					if(res.status > 0) {
						var postdata = {
							id: oid,
							uid: uid,
							img: res.url
						};
						$.post(apiurl + 'order/tradeTransfer', postdata, function(rt) {
							console.log(JSON.stringify(rt));
							if(rt && rt.status > 0) {
								$.alert('上传成功！');
								var uppic = doc.getElementById('uppic');
								var parent = uppic.parentNode.parentNode;
								parent.parentNode.removeChild(parent);
								$.fire(opener, 'refreshbuy', null);

							} else {
								$.alert('操作失败');
							}
						});
					}
				});

			}
		};
		//相册
		function galleryImg() {
			plus.gallery.pick(function(a) {
				plus.io.resolveLocalFileSystemURL(a, function(entry) {　　　　　　 //entry为图片原目录（相册）流
					uploadImg(entry.toLocalURL());
				}, function(e) {
					console.log("读取图片错误：" + e.message);
				});
			}, function(a) {}, {
				filter: "image"
			})
		}

		//拍照
		function getImage() {
			var c = plus.camera.getCamera();
			c.captureImage(function(e) {
				//存储到本地
				plus.io.resolveLocalFileSystemURL(e, function(entry) {
					uploadImg(entry.toLocalURL()); //裁剪图片，传入绝对地址
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(s) {
				console.log("error" + s);
			}, {
				filename: "_doc/head.jpg"
			})
		}
		
		
		function startPayTime(time) {
		
			var dom = doc.getElementById('paytime');
			
			if (!dom) {
				return;
			}
			
			function show() {
				
				var value = (new Date().valueOf() / 1000 | 0) - time;
				
				// 一个小时倒计时
				value = 3600 - value;
				
				if (value <= 0) {
					value = '已超时';
				} else if (value > 60) {
					value = value / 60 | 0;
					value += '分钟';
					setTimeout(show, value > 1 ? 3600 : 1000);
				} else {
					value += '秒';
					setTimeout(show, 1000);
				}
				
				dom.innerHTML = value;
			}
			
			show();
		}
		
		

		var postdata = {
			uid: uid,
			id: oid
		};
		$.post(apiurl + 'order/tradeDetail', postdata, function(res) {
			console.log(JSON.stringify(res));
			if(res.status && res.status > 0) {// res.data.status = '7'
				doc.getElementById('detail').innerHTML = template('tpl', {
					data: res.data || {},
					data1: res.data1 || {}
				});
				
				// add by yaozy 付款倒计时
				if (res.data && res.data.status === '6') {
					startPayTime(res.data.sale_time);
				}
				
				var uppic = doc.getElementById('uppic');
				uppic && uppic.addEventListener('tap', function() {
					var a = [{
						title: "拍照"
					}, {
						title: "从手机相册选择"
					}];
					plus.nativeUI.actionSheet({
						title: "上传打款凭证",
						cancel: "取消",
						buttons: a
					}, function(b) {
						switch(b.index) {
							case 0:
								break;
							case 1:
								getImage();
								break;
							case 2:
								galleryImg();
								break;
							default:
								break
						}
					})

				});
				var viewpic = doc.getElementById('viewpic');
				viewpic && viewpic.addEventListener('tap', function() {
					var img = this.getAttribute('data-src');
					//console.log(img);
					//plus.runtime.openURL(img);
					doc.getElementById('cropper').style.display = '';
					doc.getElementById('im').src = img;
				});
				var cropper = doc.getElementById('cropper');
				cropper && cropper.addEventListener('tap', function() {
					doc.getElementById('cropper').style.display = 'none';
				});
				var confirmpay = doc.getElementById('confirmpay');
				confirmpay && confirmpay.addEventListener('tap', function() {
					var btnArray = ['确定', '取消'];
					mui.prompt('请输入交易密码', '', '', btnArray, function(e) {
						if(e.index == 0) {
							if(e.value && e.value.length > 0) {
								var postdata = {
									uid: uid,
									id: oid,
									trade_pwd: e.value
								};
								$.post(apiurl + 'order/tradeRreceive', postdata, function(res) {
									console.log(JSON.stringify(res));
									if(res && res.status > 0) {
										$.alert("操作成功");
										var confirmpay = doc.getElementById('confirmpay');
										var parent = confirmpay.parentNode;
										parent.parentNode.removeChild(parent);
										$.fire(opener, 'refreshsale', null);
									} else {
										$.alert("操作失败");
									}
								});
							} else {
								$.toast("密码不能为空！");
							}
						}
					}, 'div');
				});
				var contactuser = doc.getElementById('contactuser');
				contactuser && contactuser.addEventListener('tap', function() {
					plus.device.dial(res.data1.telephone);
				});
				var confirmcoin = doc.getElementById('confirmcoin');
				confirmcoin && confirmcoin.addEventListener('tap', function() {
					var postdata = {
						uid: uid,
						id: oid
					};
					$.post(apiurl + 'order/tradeEnd', postdata, function(res) {
						console.log(JSON.stringify(res));
						if(res && res.status > 0) {
							$.alert("操作成功");
							var confirmpay = doc.getElementById('confirmcoin');
							var parent = confirmpay.parentNode;
							parent.parentNode.removeChild(parent);
							$.fire(opener, 'refreshbuy', null);
						} else {
							$.alert(res.msg);
						}
					});
				});
			
				// add by yaozy 增加取消订单功能
				var cancelorder = doc.getElementById('cancelorder');
				cancelorder && cancelorder.addEventListener('tap', function () {
					mui.prompt('请输入交易密码', '', '', ['确定', '取消'], function(e) {
						if(e.index == 0) {
							if(e.value && e.value.length > 0) {
								var postdata = {
									uid: uid,
									id: oid,
									trade_pwd: e.value
								};
								console.log(JSON.stringify(postdata));
								$.post(apiurl + 'order/tradeCancel', postdata, function(res) {
									console.log(JSON.stringify(res));
									if(res && res.status > 0) {
										$.alert("操作成功,退币到矿机钱包");
										self.close();
										$.fire(opener, 'refreshsale', null);
									} else {
										$.alert("操作失败");
									}
								});
							} else {
								$.toast("密码不能为空！");
							}
						}
					}, 'div');
				});
				
									
				// add by yaozy 增加投诉功能
				var complain = doc.getElementById('complain');
				complain && complain.addEventListener('tap', function () {
					mui.openWindow({
						url: '/pages/market/complain.html',
						id: '/pages/market/complain.html',
						extras : {
							oid : oid
						},
						show: {
							autoShow: true,
							aniShow: 'slide-in-right',
							duration: 100
						}
					});
				});
			
			} else {
				$.alert('获取数据失败');
				$.back();
			}
		});
	});

}(mui, document));